package fenetres;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

import controles.*;
import database.DBManager;





public class AddSupplierFrame extends JInternalFrame implements ActionListener
{
	JPanel entetePanel, panel1,panel2,panel3;
	JTabbedPane tabbedPane ;
	JTextField codeTxt,nomTxt,adresseTxt,telTxt;
	JButton annulerButton,enregButton;
	String nomVendeur, adresseVendeur, telVendeur;
	
	JTable produitTable;
	GenericTableModel tableModel;
	String [] titreTable = {"Code Produit","Produit","Categorie","Prix"};
	JButton supprBut,ajoutBut,validerBut,annulerBut,validerBut1,annulerBut1;
	Vector<Produit> listProduitsBase;
	//Vector<InfoProduit> listProduitStock;
	
	JTextField codeProdTxt,prixTxt;
	//Vector<InfoProduit> listeProduit;
	JComboBox nomCombo,categCombo;
	Fournisseur oFrs;
	Utilisateur userConnected;
	Vector<Categorie> listCateg;
	Vector<InfoProduit> listProduitSelected;
	
	public AddSupplierFrame (Utilisateur _userConnected)
	{
		super("Ajout Fournisseur",
	              false, //resizable
	              true, //closable
	              false, //maximizable
	              false);//iconifiable
		oFrs = new Fournisseur();
		userConnected = _userConnected;
		boolean accept = checkUserConnected();
		if (accept){
			setSize(700,600);
			setLocation(30,50);
			//elaboration de l'entete
			entetePanel = new JPanel ();
			entetePanel.setLayout(null);
			
			JLabel titleLabel = new javax.swing.JLabel("Ajout d'un nouveau Fournisseur ");
			//titleLabel.setFont(new java.awt.Font("Lucida Grande", 0, 16));
			titleLabel.setFont(new java.awt.Font("Century Schoolbook L", java.awt.Font.ITALIC, 24));
			titleLabel.setForeground(Color.BLUE);
			entetePanel.add(titleLabel);
			titleLabel.setBounds(120,20,400,25);
			entetePanel.setBounds(0,0,700,70);
			this.getContentPane().setLayout(null);
			this.getContentPane().add(entetePanel);

			creerOnglet();
		
			this.getContentPane().add(tabbedPane);
			this.setVisible(true);
		}
	}
	
	public boolean checkUserConnected(){
		//userConnected = accueil.authent.getUserConnected();
		if( userConnected == null){
			JOptionPane.showMessageDialog(null, "Aucun utilisateur connecte!! \n Veuillez connecter svp :) ","Erreur",JOptionPane.ERROR_MESSAGE);
			this.dispose();
			return false;
		}
		else {
			return true;
		}
	}
	
	public void creerOnglet(){
		tabbedPane = new JTabbedPane();
		tabbedPane.setBounds(0,70,700,730);
		
		panel1 = new JPanel ();
		panel2 = new JPanel ();
		panel3 = new JPanel ();
		
		panel1.setBounds(0,70,700,730);
		panel2.setBounds(0,70,700,730);
		panel3.setBounds(0,70,700,730);
		
		updateProfil();
		
		tabbedPane.addTab("Profil", null, panel1,
		                  "Does nothing");
		 
		/*tabbedPane.addTab("Produits", null, panel2,
		                  "Does twice as much nothing");*/
		 
//		tabbedPane.addTab("Recettes", null, panel3,
//		                  "Still does nothing");

	}
	
	public void updateProfil(){
		JPanel infoPanel = new JPanel();
		infoPanel.setLayout(null);
		infoPanel.setBounds(0,30,350,400);
		infoPanel.setBackground(Color.LIGHT_GRAY);
		JLabel piclabel1 = new JLabel();
		JLabel piclabel2 = new JLabel();
		
		try {
			BufferedImage picture1 = ImageIO.read(new File("images/frs1.png"));
			piclabel1 = new JLabel (new ImageIcon(picture1));
			BufferedImage picture2 = ImageIO.read(new File("images/frs2.png"));
			piclabel2 = new JLabel (new ImageIcon(picture2));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		}
		
		JLabel enteteProfLab = new JLabel ("Informations sur le vendeur");
		enteteProfLab.setFont(new java.awt.Font("Century Schoolbook L", java.awt.Font.BOLD, 18));
		enteteProfLab.setForeground(Color.DARK_GRAY);
		
		//recuperer le code du produit suivant
		String codeFrs = etablishNextCodeFrs();
				
		JLabel codeLab = new JLabel ("Code:");
		codeTxt= new JTextField ();
		codeTxt.setEditable(false);
		codeTxt.setText(codeFrs);
		
		JLabel nomLab = new JLabel ("Nom:");
		nomTxt= new JTextField ();
		JLabel adresseLab = new JLabel ("Adresse:");
		adresseTxt= new JTextField ();
		JLabel telLab = new JLabel ("Tél:");
		telTxt= new JTextField ();
		enregButton=new JButton("Ajouter");
		enregButton.addActionListener(this);
		
		annulerButton=new JButton("Annuler");
		annulerButton.addActionListener(this);
		annulerButton.addActionListener(this);
		panel1.setLayout(null);
		
		
		infoPanel.add(enteteProfLab);
		panel1.add(piclabel1);
		panel1.add(piclabel2);
		infoPanel.add(codeLab);
		infoPanel.add(codeTxt);
		infoPanel.add(nomLab);
		infoPanel.add(nomTxt);
		infoPanel.add(adresseLab);
		infoPanel.add(adresseTxt);
		infoPanel.add(telLab);
		infoPanel.add(telTxt);
		
		infoPanel.add(enregButton);
		infoPanel.add(annulerButton);
		panel1.add(infoPanel);
		
		enteteProfLab.setBounds(40,10,300,30);
		
		piclabel1.setBounds(380,10,250,190);
		piclabel2.setBounds(380,220,240,220);
		codeLab.setBounds(15,70,80,30);
		codeTxt.setBounds(100,70,200,30);
		nomLab.setBounds(15,120,80,30);
		nomTxt.setBounds(100,120,200,30);
		adresseLab.setBounds(15,170,80,30);
		adresseTxt.setBounds(100,170,200,30);
		telLab.setBounds(15,220,80,30);
		telTxt.setBounds(100,220,200,30);
		enregButton.setBounds(20,330,120,30);
		annulerButton.setBounds(170,330,120,30);
		
	}
	
	public String etablishNextCodeFrs(){
		String codeProd = "";
		DBManager dbManager = new DBManager();
		int idMaxProd = dbManager.getMaxtypeFrsId();
		if(idMaxProd < 10) codeProd = "FRS000"+idMaxProd;
		else if(idMaxProd<100) codeProd = "FRS00"+idMaxProd;
		else if (idMaxProd <1000) codeProd = "FRS0"+idMaxProd;
		else if(idMaxProd < 10000) codeProd = "FRS"+idMaxProd;
		return codeProd;
	}
	
			@Override
		public void actionPerformed(ActionEvent e) {
			
			if(e.getSource() == enregButton){
				Fournisseur oFrs = new Fournisseur();
				oFrs.setFourn_code(codeTxt.getText());
				oFrs.setFourn_nom(nomTxt.getText());
				oFrs.setFourn_adresse(adresseTxt.getText());
				oFrs.setFourn_tel(telTxt.getText());
				//Enregistrement dans la base de donnees
				DBManager dbManager = new DBManager();
				boolean bRes = dbManager.storeFournisseur(oFrs);
				if(bRes) {
					JOptionPane.showMessageDialog(null, "Ajout Fournisseur reussie !!!","Confirmation",JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
				}
				else JOptionPane.showMessageDialog(null, "Ajout Fournisseur echoue !!!","Erreur",JOptionPane.ERROR_MESSAGE);
				this.dispose();
			}
			else if(e.getSource() == annulerButton){
				// TODO Auto-generated method stub
				nomTxt.setText("");
				adresseTxt.setText("");
				telTxt.setText ("");
			}
		}
}